package com.unicom.modules.system.sysuser.service.impl;


import com.unicom.common.api.vo.Result;
import com.unicom.common.constant.CacheConstant;
import com.unicom.common.constant.CommonConstant;
import com.unicom.common.system.api.ISysBaseAPI;
import com.unicom.common.system.vo.LoginUser;
import com.unicom.common.system.vo.SysUserCacheInfo;
import com.unicom.common.util.oConvertUtils;
import com.unicom.modules.system.sysuser.dao.SysUserMapper;
import com.unicom.modules.system.sysuser.entity.SysUser;
import com.unicom.modules.system.sysuser.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @Author: scott
 * @Date: 2018-12-20
 */
@Service("sysUserService")
@Slf4j
public class SysUserServiceImpl  implements ISysUserService {
	
	@Autowired
	private SysUserMapper userMapper;

	@Autowired
	private ISysBaseAPI sysBaseAPI;

	
	@Override
	public SysUser getUserByName(String username) {
		return userMapper.getUserByName(username);
	}


	/**
	   * 校验用户是否有效
	 * @param sysUser
	 * @return
	 */
	@Override
	public Result<?> checkUserIsEffective(SysUser sysUser) {
		Result<?> result = new Result<Object>();
		//情况1：根据用户信息查询，该用户不存在
		if (sysUser == null) {
			result.error500("该用户不存在，请注册");
			sysBaseAPI.addLog("用户登录失败，用户不存在！", CommonConstant.LOG_TYPE_1, null);
			return result;
		}
		//情况2：根据用户信息查询，该用户已注销
		if (CommonConstant.DEL_FLAG_1.toString().equals(sysUser.getDel_flag())) {
			sysBaseAPI.addLog("用户登录失败，用户名:" + sysUser.getUsername() + "已注销！", CommonConstant.LOG_TYPE_1, null);
			result.error500("该用户已注销");
			return result;
		}
		//情况3：根据用户信息查询，该用户已冻结
		if (CommonConstant.USER_FREEZE.equals(sysUser.getStatus())) {
			sysBaseAPI.addLog("用户登录失败，用户名:" + sysUser.getUsername() + "已冻结！", CommonConstant.LOG_TYPE_1, null);
			result.error500("该用户已冻结");
			return result;
		}
		return result;
	}

	@Override
	public void updateUserDepart(String username, String orgCode) {
		userMapper.updateUserDepart(username,orgCode);
	}
}
